package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.LumiHome;
import vn.lumi.smarthome.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.astuetz.viewpager.extensions.FixedTabsView;
import com.astuetz.viewpager.extensions.TabsAdapter;
import com.astuetz.viewpager.extensions.ViewPagerTabButton;

public class MainFragment extends BaseFragment {
	public static final String TAG = "MainFragment";

	private ViewPager mViewPager;
	// private FixedTabsView mFixedTabsView;

	private static final String FLOOR_ID = "floor_id";
	private static final String ROOM_ID = "room_id";
	private static final String POS = "pos";

	// Khai bao bien luu floor_id vs room_id
	private static int mFloor_id;
	private static int mRoom_id;
	private int pos;
	public static boolean isActivityPause = false;

	// Cac bien chua thong tin ve loai man hinh
	public static int SCREEN_DEVICE = 0;
	public static int SCREEN_SCENE = 1;
	public static int SCREEN_TOTAL = 2;

	private MainFragmentPagerAdapter mMainFragmentPagerAdapter;
	private static DeviceFragment mDeviceFragment;
	private static SceneFragment mSceneFragment;

	private OnTabSelectedListener mOnTabSelectedListener;

	/**
	 * @param floor_id
	 * @param room_id
	 * @param pos
	 *            : chi ra fragment nao duoc hien thi
	 * @param isChildFragmentExist
	 *            : fragment child da ton tai hay chua
	 * @return
	 */
	public static MainFragment newInstance(int floor_id, int room_id, int pos) {
		MainFragment sMainFragment = new MainFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(FLOOR_ID, floor_id);
		bundle.putInt(ROOM_ID, room_id);
		bundle.putInt(POS, pos);
		sMainFragment.setArguments(bundle);
		return sMainFragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOnTabSelectedListener = (OnTabSelectedListener) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mFloor_id = getArguments().getInt(FLOOR_ID);
		mRoom_id = getArguments().getInt(ROOM_ID);
		pos = getArguments().getInt(POS);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_main, container, false);
		init_view(view);
		init_fragment(mFloor_id, mRoom_id);
		return view;
	}

	private void init_view(View view) {
		mViewPager = (ViewPager) view
				.findViewById(R.id.fragment_main_viewpager);
		// mFixedTabsView = (FixedTabsView) view
		// .findViewById(R.id.fragment_main_fixed_tabs);

		// mFixedTabsView.setAdapter(new FixedTabsAdapter(getActivity()));
		mMainFragmentPagerAdapter = new MainFragmentPagerAdapter(
				getFragmentManager());
		mViewPager.setAdapter(mMainFragmentPagerAdapter);
		// mFixedTabsView.setViewPager(mViewPager, mFixedTabsView);

		// TODO tungdx: modify
		// mViewPager.setViewPager(mViewPager, mOnPageChangeListener);

		mViewPager.setOnPageChangeListener(mOnPageChangeListener);
		mViewPager.setCurrentItem(pos, true);

	}

	private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			if (SCREEN_SCENE == arg0) {
				mOnTabSelectedListener.onTabSelected(TABS.SCENE);
			} else if (SCREEN_DEVICE == arg0) {
				mOnTabSelectedListener.onTabSelected(TABS.DEVICE);
			}
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};

	public void init_fragment(int floor_id, int room_id) {
		mDeviceFragment = DeviceFragment.newInstance(floor_id, room_id);
		mSceneFragment = SceneFragment.newInstance(floor_id, room_id);
	}

	@Override
	public void refreshData(int floor_id, int room_id) {
		mFloor_id = floor_id;
		mRoom_id = room_id;
		mDeviceFragment.refreshData(floor_id, room_id);
		mSceneFragment.refreshData(floor_id, room_id);
		mViewPager.setCurrentItem(SCREEN_DEVICE, true);
	}
	
	public void onlyRefreshData(int floor_id, int room_id) {
		mFloor_id = floor_id;
		mRoom_id = room_id;
		mDeviceFragment.refreshData(floor_id, room_id);
		mSceneFragment.refreshData(floor_id, room_id);
	}

	public void SwitchFrament(boolean isFragmentDevice) {
		if (isFragmentDevice)
			mViewPager.setCurrentItem(SCREEN_DEVICE, true);
		else
			mViewPager.setCurrentItem(SCREEN_SCENE, true);
	}

	public void turnAllOffOnroom(int room_id) {
		mDeviceFragment.turnAllOffOnRoom(room_id);
	}

	public void turnAllOnOnRoom(int room_id) {
		mDeviceFragment.turnAllOnOnRoom(room_id);
	}

	private static class MainFragmentPagerAdapter extends FragmentPagerAdapter {
		public MainFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			Fragment fragment;
			if (arg0 == SCREEN_SCENE) {
				fragment = mSceneFragment;
			} else {
				fragment = mDeviceFragment;
			}
			return fragment;
		}

		@Override
		public int getCount() {
			return SCREEN_TOTAL;
		}
	}

	public enum TABS {
		SCENE, DEVICE
	}

	public interface OnTabSelectedListener {
		public void onTabSelected(TABS tab);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v4.app.Fragment#onPause()
	 */
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (!isActivityPause) {
			FragmentTransaction transaction = getActivity()
					.getSupportFragmentManager().beginTransaction();
			transaction.remove(mDeviceFragment);
			transaction.remove(mSceneFragment);
			transaction.commit();
		}
	}

}
